# -*- coding: utf-8 -*-
from django import forms

from google.appengine.ext.db import djangoforms
from google.appengine.ext import db

from base.models import OneClass, Booth
from base.util.decorators import render_to, login_required, admin_required

import logging

@login_required
@render_to('booth/list.html')
def list(request):
    query = Booth.gql('order by number ASC')
    booths = query.fetch(10)
    return {'booths': booths}

class BoothForm(djangoforms.ModelForm):
    class Meta:
        model = Booth

#    def clean(self):
#        cleaned_data = self.cleaned_data
#        name = cleaned_data.get('name')
#        if School.is_existed(name):
#            raise forms.ValidationError(u'You already have the same school\'s name')
#        return cleaned_data

@admin_required
@render_to('booth/form.html')
def create(request):
    form = BoothForm()
    if request.method == 'POST':
        form = BoothForm(request.POST)
        if form.is_valid():
            booth = form.save(commit=False)
            booth.put()
            return booth.get_absolute_url
    return {'form': form}

@admin_required
@render_to('booth/detail.html')
def detail(request, booth_id):
    booth = Booth.get_using_id(int(booth_id)) or None
    return {
        'booth': booth,
        }

@admin_required
@render_to('booth/form.html')
def change(request, booth_id):
    booth = Booth.get_using_id(int(booth_id))
    form = BoothForm(request.POST or None, instance=booth)
    if form.is_valid():
        booth = form.save()
        return booth.get_absolute_url
    return {'form': form}
